//
//  MyAppointCell.m
//  yayi
//
//  Created by Veer on 16/3/23.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIMyAppointCell.h"
#import "YAYIMyAppointItem.h"


@interface YAYIMyAppointCell()

@property (nonatomic, weak) UILabel *timeLbl;

@property (nonatomic, weak) UIView *bgView;

/**
 *  退号btn
 */
@property (nonatomic, weak) UIButton *stateBtn;

@property (nonatomic, strong) NSMutableArray *headLbls;
@property (nonatomic, strong) NSMutableArray *detailLbls;

// 圆形突变
@property (nonatomic, weak) UIView *typeView;
@property (nonatomic, weak) UILabel *typeLbl;

@end

@implementation YAYIMyAppointCell


- (void)setItem:(YAYIMyAppointItem *)item
{
    _item = item;
    
    YAYILog(@"bookingtime:%@", item.bookingtime);
    if (item.bookingtime) {
        self.timeLbl.hidden = NO;
        self.timeLbl.text = item.bookingtime;

    }else{
        self.timeLbl.hidden = YES;
    }
    
    
    UILabel *lbl = nil;
    lbl = self.detailLbls[0];
    lbl.text = [NSString stringWithFormat:@"%@-%@", item.bookingtimestart, item.bookingtimeend];
    
    lbl = self.detailLbls[1];
    lbl.text = item.patientname;
    
    lbl = self.detailLbls[2];
    lbl.text = item.mobile;
    
    lbl = self.detailLbls[3];
    lbl.text = item.hospitalname;


    self.typeLbl.text = 0 == item.isfirst ? @"初诊" : @"复诊";
    
    if(1 == item.d){ // 已取消
        [self.stateBtn setTitle:@"已取消" forState:UIControlStateNormal];
        [self.stateBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
    }else if(1 == item.isorder){ // 已就诊
        [self.stateBtn setTitle:@"已就诊" forState:UIControlStateNormal];
        [self.stateBtn setTitleColor:YAYIColor(230, 97, 45) forState:UIControlStateNormal];
    }else if(0 == item.isorder){ // 未就诊
        [self.stateBtn setTitle:@"未就诊" forState:UIControlStateNormal];
        [self.stateBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }
    
    [self setupFrame];
}


+ (instancetype)cellWith:(UITableView *)tablevie
{
    static NSString *cellID = @"MyAppointCell";
    
    YAYIMyAppointCell *cell = [tablevie dequeueReusableCellWithIdentifier:cellID];
    
    if (nil == cell) {
        cell = [[YAYIMyAppointCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    return cell;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // setupSubview
        [self setupSubView];
        self.selected = NO;
    }
    return self;
}


- (void)setupSubView
{
    self.backgroundColor = [UIColor clearColor];
    
    // 添加时间label
    UILabel *timeLbl = [[UILabel alloc] init];
    timeLbl.textColor = [UIColor whiteColor];//YAYIColor(165, 165, 165);
    timeLbl.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];
    timeLbl.layer.cornerRadius = 10;
    timeLbl.layer.masksToBounds = YES;
    timeLbl.textAlignment = NSTextAlignmentCenter;
//    timeLbl.text = @"上午 10:25";
    timeLbl.font = [UIFont systemFontOfSize:13];
    [self.contentView addSubview:timeLbl];
    self.timeLbl = timeLbl;
    
    
    // 背景view
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 5;
    bgView.clipsToBounds = YES;
    [self.contentView addSubview:bgView];
    self.bgView = bgView;
    
    
    
    NSArray *titleArray = @[@"预约时间：", @"就诊人：", @"联系号码：", @"就诊地点："];
    
    NSMutableArray *headLblArray = [NSMutableArray array];
    NSMutableArray *detailLblArray = [NSMutableArray array];
    // 就诊人
    for (int i = 0; i < titleArray.count; i++) {
        // 创建head Label
        UILabel *titleLbl = [[UILabel alloc] init];
        titleLbl.textColor = [UIColor grayColor];
        titleLbl.font = [UIFont systemFontOfSize:16];
        titleLbl.text = titleArray[i];
        [bgView addSubview:titleLbl];
        [headLblArray addObject:titleLbl];
        
        //创建说明Label
        UILabel *detailLbl = [[UILabel alloc] init];
//        detailLbl.textColor = YAYIColor(71, 71, 71);
        detailLbl.font = [UIFont systemFontOfSize:16];
        detailLbl.textColor = [UIColor grayColor];
        detailLbl.text = @"detail";
        [bgView addSubview:detailLbl];
        [detailLblArray addObject:detailLbl];
        
    }
    self.headLbls = headLblArray;
    self.detailLbls = detailLblArray;
    
    // 状态，圆形图标
    UIView *typeView = [[UIView alloc] init];
    typeView.layer.borderWidth = 1.3;
    typeView.layer.borderColor = YAYIColor(92, 169, 239).CGColor;
    typeView.layer.masksToBounds = YES;
    typeView.layer.cornerRadius = 19;
    [bgView addSubview:typeView];
    self.typeView = typeView;
    
    UILabel *typeLbl = [[UILabel alloc] init];
    typeLbl.font = [UIFont systemFontOfSize:14];
    typeLbl.textColor = YAYIColor(92, 169, 239);
    typeLbl.textAlignment = NSTextAlignmentCenter;
    [typeView addSubview:typeLbl];
    self.typeLbl = typeLbl;
    
    // 状态btn
    UIButton *stateBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [stateBtn setTitle:@"已预约" forState:UIControlStateNormal];
    [stateBtn setTitleColor:YAYIColor(230, 97, 45) forState:UIControlStateNormal];
    
    stateBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    
    [stateBtn addTarget:self action:@selector(retreatClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:stateBtn];
    self.stateBtn = stateBtn;
}


- (void)setupFrame
{
    CGFloat marginLeft = 20;
    CGFloat marginTop = 15;
    CGFloat BgMarginLeft = 10;
    
    // timeLbl
    CGFloat timeY = 12;
    CGFloat timeW = 100;
    CGFloat timeH = 20;
    CGFloat timeX = (SCREEN_width - timeW) * 0.5;
    self.timeLbl.frame = CGRectMake(timeX, timeY, timeW, timeH);
    
    // bgView
    CGFloat bgX = BgMarginLeft;
    CGFloat bgY = self.item.bookingtime ? CGRectGetMaxY(self.timeLbl.frame) + 12 : 15;
    CGFloat bgW = SCREEN_width - 2 * BgMarginLeft;
    
    for (int i = 0; i < self.headLbls.count; i++) {
        UILabel *lbl = self.headLbls[i];
        CGFloat lblW = 80;
        CGFloat lblH = 30;
        CGFloat lblX = marginLeft;
        CGFloat lblY = marginTop + lblH * i;
        
        lbl.frame = CGRectMake(lblX, lblY, lblW, lblH);
        
        UILabel *detailLbl = self.detailLbls[i];
        CGFloat detailLblX = CGRectGetMaxX(lbl.frame);
        CGFloat detailLblY = lblY;
        CGFloat detailLblW = bgW - detailLblX;
        CGFloat detailLblH = lblH;
        detailLbl.frame = CGRectMake(detailLblX, detailLblY, detailLblW, detailLblH);
    }
    
    UILabel *lastLbl = [self.headLbls lastObject];
    // btn2
    CGFloat btn2W = 75;
    CGFloat btn2H = 30;
    CGFloat btn2X = bgW - marginLeft - btn2W + 10;
    CGFloat btn2Y = CGRectGetMaxY(lastLbl.frame) + marginTop;
    self.stateBtn.frame = CGRectMake(btn2X, btn2Y, btn2W, btn2H);
    
    // 圆形类型图标
    self.typeView.frame = CGRectMake(bgW - marginLeft - 38 - 5, marginTop + 5, 38, 38);
    self.typeLbl.frame = CGRectMake(0, 0, 38, 38);
    
    
    CGFloat bgH = CGRectGetMaxY(self.stateBtn.frame) + marginTop;
    self.cellHeight = bgH;
    self.bgView.frame = CGRectMake(bgX, bgY, bgW, bgH);
}


//- (void)layoutSubviews
//{
//    [super layoutSubviews];

//    CGFloat marginLeft = 20;
//    CGFloat marginTop = 15;
//    CGFloat BgMarginLeft = 10;
//    // timeLbl
//    CGFloat timeX = 0;
//    CGFloat timeY = 0;
//    CGFloat timeW = self.frame.size.width;
//    CGFloat timeH = 38;
//    self.timeLbl.frame = CGRectMake(timeX, timeY, timeW, timeH);
//
//    // bgView
//    CGFloat bgX = BgMarginLeft;
//    CGFloat bgY = CGRectGetMaxY(self.timeLbl.frame);
//    CGFloat bgW = self.frame.size.width - 2 * BgMarginLeft;
//    
//    for (int i = 0; i < self.headLbls.count; i++) {
//        UILabel *lbl = self.headLbls[i];
//        CGFloat lblW = 80;
//        CGFloat lblH = 30;
//        CGFloat lblX = marginLeft;
//        CGFloat lblY = marginTop + lblH * i;
//        
//        lbl.frame = CGRectMake(lblX, lblY, lblW, lblH);
//        
//        UILabel *detailLbl = self.detailLbls[i];
//        CGFloat detailLblX = CGRectGetMaxX(lbl.frame);
//        CGFloat detailLblY = lblY;
//        CGFloat detailLblW = bgW - detailLblX;
//        CGFloat detailLblH = lblH;
//        detailLbl.frame = CGRectMake(detailLblX, detailLblY, detailLblW, detailLblH);
//    }
//    
//    UILabel *lastLbl = [self.headLbls lastObject];
//    // btn2
//    CGFloat btn2W = 75;
//    CGFloat btn2H = 30;
//    CGFloat btn2X = bgW - marginLeft - btn2W;
//    CGFloat btn2Y = CGRectGetMaxY(lastLbl.frame) + marginTop;
//    self.retreatBtn.frame = CGRectMake(btn2X, btn2Y, btn2W, btn2H);
//    
//    // 圆形类型图标
//    self.typeView.frame = CGRectMake(bgW - marginLeft - 38 - 5, marginTop + 5, 38, 38);
//    self.typeLbl.frame = CGRectMake(0, 0, 38, 38);
//    
//    
//    CGFloat bgH = CGRectGetMaxY(self.retreatBtn.frame) + marginTop;
//    self.cellHeight = bgH;
//    self.bgView.frame = CGRectMake(bgX, bgY, bgW, bgH);
//}


- (void)retreatClick:(UIButton *)btn
{
    if (self.retreatOperation) {
        self.retreatOperation(self.indexPath, btn);
    }
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
